package com.efrei.schoolmanager.views;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.efrei.schoolmanager.exceptions.LogicException;
import com.efrei.schoolmanager.model.obj.Classe;
import com.efrei.schoolmanager.tools.Tools;
import com.efrei.schoolmanager.views.components.CustomButton;
import com.efrei.schoolmanager.views.components.CustomTextField;

/**
 * JDialog pour la modification d'une classe
 * 
 */
public class DialogModifierClasse extends JDialog {
	private static final long serialVersionUID = 568966991463817875L;

	private final static String TITLE = "Modifier la classe";

	// Proprietaire de cette dialog(objectif: rendre modale la jdialog)
	private final JFrame proprio;

	// JPanels
	private JPanel titlePanel, formulairePanel, butonPanel;

	// JLabels
	private JLabel titleLabel, nomClasseLabel, promotionClasseLabel;

	// CustomTextField
	private CustomTextField nomClasseTextField, promotionClasseTextField;

	// CustomButton
	private CustomButton modifierClasseButton;

	private final Classe classeSelectionee;

	// Pour fermer la jdialog
	private final DialogModifierClasse dialog;

	public DialogModifierClasse(JFrame frame, Classe classeSelectionee) {
		super(frame, TITLE, true);
		proprio = frame;

		dialog = this;

		this.classeSelectionee = classeSelectionee;
		initLabels();
		initPanels();
		initTextFields();
		initButtons();
		remplirTitlePanel();
		remplirFormulairePanel();
		remplirButonPanel();
		configurerVue();
	}

	private void remplirButonPanel() {
		butonPanel = new JPanel(new FlowLayout());
		butonPanel.add(modifierClasseButton);
	}

	private void remplirFormulairePanel() {
		// Gestionnaire de positionnement
		GroupLayout layout = new GroupLayout(formulairePanel);
		formulairePanel.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		hGroup.addGroup(layout.createParallelGroup()
				.addComponent(nomClasseLabel)
				.addComponent(promotionClasseLabel));
		hGroup.addGroup(layout.createParallelGroup()
				.addComponent(nomClasseTextField)
				.addComponent(promotionClasseTextField));
		layout.setHorizontalGroup(hGroup);

		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
				.addComponent(nomClasseLabel).addComponent(nomClasseTextField));
		vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
				.addComponent(promotionClasseLabel)
				.addComponent(promotionClasseTextField));
		layout.setVerticalGroup(vGroup);
	}

	private void remplirTitlePanel() {
		titlePanel.add(titleLabel);
	}

	/**
	 * Configurer les options de la vue
	 * 
	 */
	private void configurerVue() {
		setLayout(new BorderLayout());
		this.add(titlePanel, BorderLayout.NORTH);
		this.add(formulairePanel, BorderLayout.CENTER);
		this.add(butonPanel, BorderLayout.SOUTH);
		Dimension preferedSize = getPreferredSize();
		Dimension dimensionIdeale = new Dimension(preferedSize.width + 80,
				preferedSize.height + 28);
		setMinimumSize(dimensionIdeale);
		setSize(dimensionIdeale);
		this.setLocationRelativeTo(proprio);
	}

	private void initButtons() {
		modifierClasseButton = new CustomButton("Modifier la classe");
		modifierClasseButton
				.addActionListener(new ModifierClasseFinishActionListener());
	}

	private void initTextFields() {
		nomClasseTextField = new CustomTextField(classeSelectionee.getNom());
		promotionClasseTextField = new CustomTextField(
				String.valueOf(classeSelectionee.getPromotion()));

	}

	private void initPanels() {
		titlePanel = new JPanel(new FlowLayout());
		formulairePanel = new JPanel();
		butonPanel = new JPanel(new FlowLayout());
	}

	private void initLabels() {
		titleLabel = new JLabel(TITLE);
		nomClasseLabel = new JLabel("Nom : ");
		promotionClasseLabel = new JLabel("Promotion : ");

	}

	public boolean showDialog() {
		setVisible(true);
		return true;
	}

	/**
	 * Listener : lorsque l'utilisateur valide la modification de la classe, la
	 * fenetre se ferme, et on modifie la classe
	 * 
	 */
	class ModifierClasseFinishActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String error = null;

			String nom = null;
			String promotionText = null;
			int promotion;
			try {
				// Verifier le nom
				nom = nomClasseTextField.getText();
				if (nom == null || nom.equals(""))
					throw new LogicException("Nom vide");
				// Verifier la promotion
				promotionText = promotionClasseTextField.getText();
				if (promotionText == null || promotionText.equals(""))
					throw new LogicException("Promotion vide");
				// Parser le champ pour optenir un entier
				promotion = Integer.parseInt(promotionText);
				if (promotion < 1)
					throw new LogicException("Promotion < 1");

				// Modifier les informations
				classeSelectionee.setNom(nom);
				classeSelectionee.setPromotion(promotion);
			} catch (NumberFormatException e) {
				System.err.println(e);
				error = "Nombre attendu";
			} catch (LogicException e) {
				System.err.println(e);
				error = e.getMessage();
			}
			if (error != null) {
				Tools.displayError(error, "Erreur");
			} else {
				dialog.setVisible(false);
			}
		}
	}

}
